<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of slide
 *
 * @author b4by
 */
class image extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('model_file');
		$this->load->model('model_image');
	}

	public function index() {
		$this->data['temp'] = 'admin/image/index';
		$this->load->view('admin/main', $this->data);
	}

	public function read() {
		$pagination = pagination_init($this->model_image->get_total(), request('p', 1));
		$images = $this->model_image->get_list(array(
			'limit' => array($pagination->from, $pagination->limit)
				));
		foreach($images as &$image){
			$image->thumb = image_url($image->thumb);
			$image->edit_link = url_create('admin','image','update',$image->id);
			if($image->position==0) $image->position=lang('disable');
			if($image->position==1) $image->position=lang('slide');
			if($image->position==2) $image->position=lang('banner_left');
			if($image->position==3) $image->position=lang('logo');
			if($image->position==4) $image->position=lang('banner_top');
			if($image->position==5) $image->position=lang('banner_bottom');
		}
		echo json_encode(array(
			'state' => 'success',
			'data' => $images,
			'pagination'	=> $pagination,
		));
	}

	public function create() {
		
		if ($this->input->post('submit')) {
			$this->load->library('form_validation');
			$this->form_validation->set_rules('image_id', 'lang:image_id', 'required|callback__check_image_id');
			if ($this->form_is_valid()) {
				$data = request_filter(array('image_id', 'link', 'target','position'));
				$image = $this->model_file->get_info($data['image_id']);
				$data['image'] = $image->file_name;
				$data['thumb'] = $image->thumb_name;
				$this->model_image->create($data);
				$this->model_file->update($image->id, array('status' => 1));
				echo json_encode(array(
					'state' => 'success',
					'message' => 'create_success',
					'redirect'	=> url_create('admin','image','index')
				));
			} else {
				echo json_encode(array(
					'state' => 'error',
					'errors' => $this->data['errors'],
				));
			}
			exit();
		}
		$this->data['temp'] = 'admin/image/detail';
		$this->load->view('admin/main', $this->data);
	}

	public function update($id) {
		$image = $this->model_image->get_info($id);
		if(!$image) show_error(lang('image_not_found'));
		$this->data['data'] = $image;
		if ($this->input->post('submit')) {
			$this->load->library('form_validation');
			$this->form_validation->set_rules('image_id', 'lang:image_id', 'required|callback__check_image_id');
			if ($this->form_is_valid()) {
				$data = request_filter(array('image_id', 'link', 'target','position'));
				if ($data['image_id'] != $image->image_id) {
					$file = $this->model_file->get_info($data['image_id']);
					$data['image'] = $file->file_name;
					$data['thumb'] = $file->thumb_name;
					$this->model_file->update($file->id, array('status' => 1));
					$this->model_file->update($file->id, array('status' => 1));
				}
				
				$this->model_image->update($id,$data);
				
				echo json_encode(array(
					'state' => 'success',
					'message' => 'create_success',
					'redirect'	=> url_create('admin','image','index')
				));
			} else {
				echo json_encode(array(
					'state' => 'error',
					'errors' => $this->data['errors'],
				));
			}
			exit();
		}
		$this->data['temp'] = 'admin/image/update';
		$this->load->view('admin/main', $this->data);
	}
	public function delete(){
		$id = request('id');
		$image = $this->model_image->get_info($id);
		if(!$image){
			echo json_encode(array(
				'state'	=> 'error'
			));
		}
		$this->model_file->update($image->image_id,array('status'=>0));
		
		$this->model_image->del($id);
		echo json_encode(array(
			'state'	=> 'success',
			'id'=> $id,
		));
	}

	public function _check_image_id($id)
	{
		$count = $this->model_file->get_total(array(
			'id'	=> $id
//			'status'=> 0,
//			'time >' => now()-60*60,
		));
		if($count>0) return TRUE;
		$this->form_validation->set_message(__FUNCTION__,lang('invalid_image'));
		return FALSE;
	}
}

?>
